﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Reflection;

namespace SimpleRest.Core.Extensions.Routes
{
    public class DescriptionRouteHandler : IRouteHandler
    {
        public readonly Type ServiceType;
        public readonly Route AssociatedRoute;
        public readonly Configuration Configuration;
        public Pipeline RoutePipeline { get; private set; }

        public DescriptionRouteHandler(Pipeline routePipeline, Route associatedRoute, Configuration configuration)
        {
            this.RoutePipeline = routePipeline;
            this.AssociatedRoute = associatedRoute;
            this.Configuration = configuration;
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            if (requestContext.HttpContext.Request.HttpMethod.ToLower().Equals("options"))
            {
                return new DescriptionHttpHandler(this.RoutePipeline, this.Configuration, requestContext);
            }
            else
            {
                // in the event that the request should not be routed to the operation xsd http handler then
                // reroute this request to the original operation route associated to this XSD route.
                return AssociatedRoute.RouteHandler.GetHttpHandler(requestContext);
            }
        }
    }
}
